package com.fly.studio.home.app.arrange.base;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Worker Factory
 *
 * @author fuliying3
 * @date 2022/08/23 14:22:00
 */
@Component
@Slf4j
public class WorkerFactory {

    private static final Map<String, BaseWorker> WORKER_MAP = new HashMap<>(16);

    public WorkerFactory(List<BaseWorker> baseWorkers) {
        for (BaseWorker baseWorker : baseWorkers) {
            WORKER_MAP.put(baseWorker.getClass().getSimpleName(), baseWorker);
        }
        log.info("WorkerFactory worker-map -> {}", JSON.toJSONString(WORKER_MAP));
    }

    /**
     * Get Handler
     *
     * @param handlerName worker name
     * @return {@link BaseWorker}
     */
    public BaseWorker getHandler(String handlerName) {
        return WORKER_MAP.get(handlerName);
    }
}
